
<?php
include_once 'parsers/parser.php';
include_once 'classes/News.php';
include_once 'functions/calculateWeight.php';
include_once 'functions/NewsSort.php';
include_once 'functions/printNews.php';
include_once 'functions/searchString.php';
include_once 'functions/getAllNews.php';
include_once 'functions/getNewsTitles.php';

function getTrendingWords(){
	$arrayAllNews = getAllNews();
	$arrayAllTitles = getNewsTitles($arrayAllNews);
	$arrayAllTitlesWithOutTags = stripTagsFromTitleArray($arrayAllTitles);
	$arrayAllTitlesWOUnwatedWords = removeUnwantedWords($arrayAllTitlesWithOutTags);
	$trendingWordsArray = keyWordsCount($arrayAllTitlesWOUnwatedWords);
	$SortedTrendingWordsArray = sortTWArray($trendingWordsArray);
	
	
	return($SortedTrendingWordsArray);
}


function stripTagsFromTitleArray($array){
	$arrayWOTags = array();
	$index = 0;
	foreach($array as $title){
		$titleWithOutTags = strip_tags($title);
		$arrayWOTags[$index] = $titleWithOutTags;
		$index++;
	}	
	
	return($arrayWOTags);
}

function removeUnwantedWords($array){
	$filteredArray = array();
	$index = 0;
	$unwantedWordsArray = array(",",":","\"","." , "!"," en "," sobre ", " dos "," contra "," tras ","Una "," aún "," ante "," es "," lo "," al "," A "," del ","De "," se ","Se "," una "," más ","En "," la ","La "," las ","Las "," los ","Los "," le ","Le "," un ","Un "," que ","Que "," su ","Su "," por "," a "," el ","El "," de "," ellos "," no ","No "," con "," para "," por "," segun "," mas "," entre " , " y "," sus "," así ","Así "," Me "," me "," Fue "," fue "," Tres "," tres "," Dijo "," dijo ");
	foreach($array as $title){
		$filteredTitle = str_replace($unwantedWordsArray, " ", $title);
		$filteredArray[$index] = $filteredTitle;
		$index++;
	}
	
	return($filteredArray);
}

function keyWordsCount($arrayTitles){
	
	$trendingWordsArray = array(
										"words" => array(),
										"hits" => array(),									
										);
										
	foreach($arrayTitles as $title){
		$titleWordsArray = explode(" ",$title);
		foreach($titleWordsArray as $word){
			$wordFound = FALSE;
			$sizeLimit = count($trendingWordsArray["words"]);
			$wordSize = strlen($word);
			if ($wordSize < 2){
			}
			else{
				if ($sizeLimit == 0){
					$trendingWordsArray["words"][0] = $word;
					$trendingWordsArray["hits"][0] = 1;
				}
				else{
				
					$index = 0;
					while(!$wordFound && $index < $sizeLimit){
						
						if ($word == $trendingWordsArray["words"][$index]){
							$wordFound = TRUE;
							$trendingWordsArray["hits"][$index] = $trendingWordsArray["hits"][$index] + 1;
						}
						$index++;
					}
					
					if (!$wordFound){
						$trendingWordsArray["words"][] = $word;
						$trendingWordsArray["hits"][] = 1;
					}				
				}
			}
		}
	}
	
	return($trendingWordsArray);
}

function sortTWArray($array){
    $count = count($array["hits"]);
    for($i=1; $i<$count; $i++){
        $tmp = $array["hits"][$i];
        $tmpW = $array["words"][$i];
        for ($j=$i-1; $j>=0 && $array["hits"][$j] < $tmp; $j--){
            $array["hits"][$j+1] = $array["hits"][$j];
            $array["words"][$j+1] = $array["words"][$j];
        }
        $array["hits"][$j+1] = $tmp;
        $array["words"][$j+1] = $tmpW;
    }
    return $array;
}


?>
